// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    money: number = 10000;
    count: number = 10; //交换次数
    canChoose: boolean = false; //是否可以选
    poker: string = "6";
    pokerArr: string[] = ['K', "A", "6", "2"];
    multiple: number = 1; //下注的倍数
    speed: number = 0.4;
    times: number = 6;
    start () {
        this.money = this._args.money;
        this.speed = this._args.speed;
        this.times = this._args.times;
        this.count = this._args.count;
        this.addBtn();
        this.openFun();
    }
    addBtn() {
            this.nodes.note.getComponent(cc.Label).string = `请老板不要眨眼`;
            //生成牌号
            this.poker = this.pokerArr[ccs.gf.randomInt(0,3)];
            this.scheduleOnce(() => {
                this.getGamble();
                this.nodes.note.getComponent(cc.Label).string = `请选择${this.poker}的牌`;
            }, 2);
    }
    getGamble() {
        this.nodes.ani.children.forEach( val => {
            this.startAni(val);
        })
        this.scheduleOnce(() => {
            this.nodes.ani.getComponent(cc.Layout).type = null;   
            this.changePosition();
        }, 2.5);
    }
    startAni(node: cc.Node) {
        //先缩小，再放大
        let tween = cc.tween()
        .to(1, {opacity: 0, scale: 0, angle: 360})
        .call(() => {
            node.getChildByName("font").active = false;
            node.getChildByName("icon").active = true;
        })
        let trunOut = cc.tween()
        .to(1, {angle: 0, opacity: 255, scale: 1});
        cc.tween(node).then(tween).then(trunOut).start();
    }
    changePosition() {
        //几次
        let arr = [0, 1, 2, 3];
        let start = arr.splice(ccs.gf.randomInt(0, 3), 1)[0];
        let one = this.nodes.ani.children.slice(start, start + 1)[0];
        let end = arr[ccs.gf.randomInt(0, 2)];
        let two = this.nodes.ani.children.slice(end, end + 1)[0];
        this.move(one, two)
    }
    move(node: cc.Node, changeNode: cc.Node) {
        let one = node.getPosition();
        let two = changeNode.getPosition();
        //交换动画
        cc.tween(node)
        // @ts-ignore
        .to(this.speed, {position: two})
        .start();

        cc.tween(changeNode)
        // @ts-ignore
        .to(this.speed, { position: one})
        .call(() => {
            if(this.count > 0) {
                this.count --;
                this.changePosition();
            } else {
                this.canChoose = true;
                
                // this.nodes.ani.getComponent(cc.Layout).type = cc.Layout.Type.HORIZONTAL;
            }
        }).
        start();
    }
    openFun() {
        this.nodes.ani.children.forEach( val => {
            this.touchEndEventScale(val, () => {
                if(this.canChoose) {
                    this.canChoose = false;
                    val.getChildByName('font').active = true;
                    val.getChildByName('icon').active = false;
                    this.scheduleOnce(() => {
                        this.nodes.ani.children.forEach(item => {
                            item.getChildByName('font').active = true;
                            item.getChildByName('icon').active = false;
                        })
                    }, 0.5);
                    //判断是否对了
                    if(val.getChildByName('font').getChildByName("tips").getComponent(cc.Label).string == this.poker) {
                        //对了
                        this.nodes.note.getComponent(cc.Label).string = `恭喜你赢了，获得${ this.money }的奖励`;
                        game.recordMgr.addList({name: "你", action: `获得${ this.money }的奖励`, location: "东华娱乐城"});
                        game.playerMgr.calMoney(this.money * 2);
                        
                    } else {
                        this.nodes.note.getComponent(cc.Label).string = `不好意思，你输了，共${ this.money }元`;
                        game.recordMgr.addList({name: "你", action: `输的只剩三角裤`, location: "东华娱乐城", effect: `金钱 -${ this.money }元`});
                    }
                    this.scheduleOnce(() => {
                        if(game.playerMgr.heroInfo.money <= 0) {
                            this._close();
                        } else {
                            this._close();
                            ccs.winMgr.showWin("gambleGuessWin", {type: "guess"});
                        }
                    }, 2)
                }
            })
        })
    }
    // update (dt) {}
}
